<?php
session_start();
function getlang(){
		$lang = $_SESSION['lang'];
		if (empty($lang) || $lang == "EN") {
			$value = "../../value/value_en.php";
		} else if($lang == "VN") {
			$value = "../../value/value_vn.php";
		}
		else {
			$value = "../../value/value_kr.php";
		}
		return $value;
	}
include '../../operations/connection.php';	
$value = getlang();
$list=$_SESSION['sort'];
include($value);
	$page=$_GET['page'];
	$str=$_GET['str'];
	$_SESSION['sortn']=$str;
	$co=$_GET['co'];
	//echo $co."-".$page."-".$str;
	$rows_per_page = $_SESSION['rows_page'] ;
	$page_start = ( $page - 1 ) * $rows_per_page; 
	$page_end = $page * $rows_per_page;
	$id_employee=$_SESSION['IDEmployee'];
	if($co==1){

		if($list[$str]=="DESC")
			$list[$str]="ASC";
		else
			$list[$str]="DESC";
	}
    $tile=$_REQUEST['titleSearch'];
    $fromdate=$_REQUEST['date1'];
    $todate=$_REQUEST['date2'];
    $search=$_REQUEST['search'];
	if ($search!=''){

    	$sql="SELECT SumMoney,IDApproveDoc,Tile,IDTypeDoc,DATE_FORMAT(a.TimeApprove, '%Y-%m-%d') as TimeApprove,DATE_FORMAT(a.Timecreate, '%Y-%m-%d') as Timecreate   
    	FROM approvedoc a INNER JOIN listapprove b ON a.IDGroupApprove = b.IDGroupApprove 
    	WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(".$id_employee."  USING UTF8) 
    	AND a.Tile LIKE '%$tile%' 
    	AND a.Timecreate>'".$fromdate." 00:00:00' AND a.Timecreate<'".$todate." 23:59:59' 
    	AND CONVERT(a.IDTypeDoc USING UTF8)   != 11 
    	AND CONVERT(a.IDStatus USING UTF8) = 6 
    	AND CONVERT(b.IDStatus USING UTF8) = 1 ORDER BY ".$str." ".$list[$str];

	}else{
    	$sql="SELECT SumMoney,IDApproveDoc,Tile,IDTypeDoc,DATE_FORMAT(a.TimeApprove, '%Y-%m-%d') as TimeApprove,DATE_FORMAT(a.Timecreate, '%Y-%m-%d') as Timecreate    FROM approvedoc a INNER JOIN listapprove b ON a.IDGroupApprove = b.IDGroupApprove WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(".$id_employee."  USING UTF8) AND CONVERT(a.IDTypeDoc USING UTF8)   != 11 AND CONVERT(a.IDStatus USING UTF8) = 6 AND CONVERT(b.IDStatus USING UTF8) = 1 ORDER BY ".$str." ".$list[$str];
    	
   }
    	$query  = mysql_query($sql);
    	//echo $sql;
    	$number_of_page = ceil ( mysql_num_rows( $query ) / $rows_per_page ); 

	$_SESSION['sort']=$list;
?>
<table width="100%" class="table_th" style="height:22px;" cellspacing="0">
							
							<tr class="trtit" style="background-color:#EEE;">
								<td width="3%"><?=$l_no?></td>
								<!--td width="10%"><a onclick="sort(<?=$page?>,'a.IDApproveDoc','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')">ID</a></td-->
					            <td width="15%"><?=$l_typedoc?></td>
					            <td width="30%"><a onclick="sort(<?=$page?>,'a.Tile','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><?=$l_title?></a></td>
					 
					            <td width="10%"><a onclick="sort(<?=$page?>,'a.Timecreate','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><?=$l_created_date?></a></td>
					            <td width="10%"><a onclick="sort(<?=$page?>,'a.TimeApprove','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><?=$l_approve_date?></a></td>
							
					          
					            
					        </tr>
							
							
							
							<?php
							
						    $i = 0; 
						    while ($row=mysql_fetch_array($query)){
						    	if ( $i >= $page_start  ) 
								{	
							    	if($row[IDTypeDoc]==7)$document = $l_vacation_form;
								    else if($row[IDTypeDoc]==12)$document = $l_assignment_report;
								    else if($row[IDTypeDoc]==13)$document = $l_purchase_order;
								    else if($row[IDTypeDoc]==14)$document = $l_daily_report;
								    else if($row[IDTypeDoc]==15)$document = $l_business_trip_report;
								    else if($row[IDTypeDoc]==16)$document = $l_weekly_report;
								    else if($row[IDTypeDoc]==17)$document = $l_susqence_payment_request;
							    
							?>
								<tr bgcolor="#ffffff" class="highlighter">
									<td width="3%" align="center"><?=($i+1)?></td>
									<!--td ><a href="mybin.php?id=<?php echo $row[IDApproveDoc]?>&type=<?php echo $row[IDTypeDoc]?>"><?php echo $row[IDApproveDoc];?></a></td-->
									<td ><div  align="left" ><?php echo $document;?></div></td>
									<td ><div  align="left" ><a href='mybin.php?id=<?=$row[IDApproveDoc]?>&type=<?=$row[IDTypeDoc]?>&page=<?=$page?>'><?=$row[Tile]?></a>
										<p style="float:right">
															<?php 
																if($row[IDTypeDoc]==13 || $row[IDTypeDoc]==15 || $row[IDTypeDoc]==17){
																	if($row[SumMoney]>0){
																		echo " [ ".number_format($row[SumMoney])." ]";
																	}else{
																		if($row[IDTypeDoc]==15 || $row[IDTypeDoc]==13){
																			$s1="select sum(buying_document.total) as tongtien from approvedoc,buying_document where approvedoc.IDApproveDoc=buying_document.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
																			$k1=mysql_query($s1);
																			while ($r1=mysql_fetch_array($k1)){
																				if($r1[tongtien]>0){
																					echo " [ ".number_format($r1[tongtien])." ]";
																				}
																			}
																		}else if($row[IDTypeDoc]==17){
																			$s1="select sum(ap_advance.money) as tongtien from approvedoc,ap_advance where approvedoc.IDApproveDoc=ap_advance.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
																			$k1=mysql_query($s1);
																			while ($r1=mysql_fetch_array($k1)){
																				if($r1[tongtien]>0){
																					echo " [ ".number_format($r1[tongtien])." ]";
																				}
																			}
																		}
																	}
																}
															?>
															</p>
									</div></td>
									<td ><?=$row[Timecreate]?></td>
									<td ><?=$row[TimeApprove]?></td>
									
								</tr>
							<?php 	}
							
								$i++; 
								
								if ($i >= $page_end) 
								{ 
								break; 
								} 
							}
							
								
							?>
						
						<tr>
								<td colspan="5" class="bt_td_main">
								
								<span style="background:#fff; padding:1px; display: inline-block;">
														
														<?php if ( $number_of_page > 1 ) 
														{ 
															if ($page==1)
																$p=2;
															else if ($page==$number_of_page)
																$p=$number_of_page-1;
															else $p=$page;
															if($page!=1){?>
																<a onclick="sort(1,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class='button_next'><?php echo "<<<";?></span> </a>
																<a onclick="sort(<?=($page-1)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class='button_next'><?php echo "<<";?></span> </a>
															<?php  }
															else{
																echo "<span class='button_next'><<<</span>"; 
																echo "<span class='button_next'><<</span>";
															}
															for ( $i = $p-1; $i <= $p+1; $i++ ) {	
																	if ($i<=$number_of_page && $i>0)
																	
																		if ($i==$page){
																			echo "<span class='button_next'><b>".$i."</b></span>"; 
																		}else{
																		?>
																			<a onclick="sort(<?=$i?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class="button_next">
																			<?php 
																			echo $i;
																			?>
																			</span> </a>
																		<?php 
																		}
																	?>
																
															<?php 
																	}
																
																if($page!=$number_of_page){ ?>
																	<a onclick="sort(<?=($page+1)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><span class='button_next'><?php echo ">>";?></span> </a>
																	<a onclick="sort(<?=($number_of_page)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class='button_next'><?php echo ">>>";?></span> </a>
																<?php }
																else{
																	echo "<span class='button_next'>>></span> ";
																	echo "<span class='button_next'>>>></span> ";
																}
															}
														
															
															?>
																												
													</span>
								</td>
							</tr>
						</table>
							<?php mysql_close($connection);?>